<?php

/*
system controller:系统参数设置（货品类别，供应商，物料种类，出入库类别，），包括：
用户登录检查
用户权限检查
管理用户，增、删、查、改
*/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class System extends CI_Controller {

	public $data;

	public function __construct()
	{
		parent::__construct();
		//载入必要类文件
		$this->load->model('User_model');
		$this->load->model('Right_model');
		$this->load->model('Goods_type_model');
		$this->load->model('Cost_type_model');
		$this->load->model('Inbound_type_model');
		$this->load->model('Outbound_type_model');
		//载入必要库文件
		$this->load->library('Access_control');
		$this->load->library('Menu_control');
		$this->load->library('Forbid_resubmit');
		//载入配置文件 并 初始化控制器
		$this->data['site'] = $this->config->item('site');
		$this->data['title'] = '系统设置';
		$this->data['styles'] = array('system.css', 'error.css');
		$this->data['scripts'] = array('master.js', 'system.js');
		//检查是否是登录用户
		if (!$this->User_model->checkLoginedUser())
		{
			$this->data['error'] = '登录超时，请重新登录';
			$this->load->view_('error_500_view', $this->data);
			return;
		}
		//设置站点基本参数
		$this->data['cur_user'] = $this->User_model->getUser($this->input->cookie('user_id'));
		$this->data['header']['username'] = $this->input->cookie('realname');
		$this->data['site']['environment'] = $this->User_model->get_envrionment();
		$this->data['menus'] = $this->User_model->userMenus($this->config->item('menus'), $this->data['cur_user']['rights']);
		$this->data['menus'] = $this->menu_control->main($this->data['menus'], 9);
		$this->data['sub_menus'] = $this->_menus();
		$this->data['sub_menu_index'] = '-1';
		//全局访问控制
		if (!$this->access_control->base(0, $this->data['cur_user']['rights']))
		{
			$this->data['error'] = '拒绝访问';
			$this->load->view_('error_500_view', $this->data);
		}
	}

	private function _menus()
	{
		return array(
					array(
						'index'	=>	1,
						'url'	=>	"{$this->data['site']['root']}system/goods/",
						'name'	=>	"货品类目"),
					/***
					array(
						'index'	=>	2,
						'url'	=>	"{$this->data['site']['root']}system/cost/",
						'name'	=>	"费用类别")
					***/
			);
	}

	private function _goods_menus()
	{
		return array(
				array(
					'url'	=>	"{$this->data['site']['root']}system/goods/list/",
					'name'	=>	"所有货品类目"),
				array(
					'url'	=>	"{$this->data['site']['root']}system/goods/create/",
					'name'	=>	"新建货品类目")
			);
	}

	private function _cost_menus()
	{
		return array(
				array(
					'url'	=>	"{$this->data['site']['root']}system/cost/list/",
					'name'	=>	"所有费用类别"),
				array(
					'url'	=>	"{$this->data['site']['root']}system/cost/create/",
					'name'	=>	"新建费用类别")
			);
	}

	/***
	private function _inbound_menus()
	{
		return array(
				array(
					'url'	=>	"{$this->data['site']['root']}system/inbound/list/",
					'name'	=>	"所有入库原因"),
				array(
					'url'	=>	"{$this->data['site']['root']}system/inbound/create/",
					'name'	=>	"新建入库类别")
			);
	}

	private function _outbound_menus()
	{
		return array(
				array(
					'url'	=>	"{$this->data['site']['root']}system/outbound/list/",
					'name'	=>	"所有出库原因"),
				array(
					'url'	=>	"{$this->data['site']['root']}system/outbound/create/",
					'name'	=>	"新建出库类别")
			);
	}
	***/

	public function index()
	{
		//$this->data['index']['menus'] = array_merge($this->_goods_menus(), $this->_cost_menus(), $this->_inbound_menus(), $this->_outbound_menus());
		//$this->data['index']['menus'] = array_merge($this->_goods_menus(), $this->_cost_menus());
		$this->data['index']['menus'] = array_merge($this->_goods_menus());
		$this->data['title'] = '系统管理';
		$this->load->view_('system_index_view', $this->data);
	}

	public function goods($action = 'list', $id = false)
	{
		//初始化控制器方法变量
		$this->data['site']['method'] = __FUNCTION__;
		$this->data['sub_menu_index'] = 1;
		$this->data['index']['title'] = '货品类目';
		//类目管理子菜单
		$this->data['index']['menus'] = array(
				array(
					'url'	=>	$this->data['site']['root'] . 'system/goods/list/',
					'name'	=>	'可选类目'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/goods/listRecover/',
					'name'	=>	'回收类目'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/goods/create/',
					'name'	=>	'创建类目')
			);
		$action = '_' . $action;
		$this->$action('Goods_type_model', $id);
	}

	public function cost($action = 'list', $id = false)
	{
		$this->data['site']['method'] = __FUNCTION__;
		//初始化控制器方法变量
		$this->data['sub_menu_index'] = 2;
		//
		$this->data['index']['title'] = '费用类型';
		//类目管理子菜单
		$this->data['index']['menus'] = array(
				array(
					'url'	=>	$this->data['site']['root'] . 'system/cost/list/',
					'name'	=>	'可选费用类型'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/cost/listRecover/',
					'name'	=>	'回收费用类型'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/cost/create/',
					'name'	=>	'创建费用类型')
			);
		$action = '_' . $action;
		$this->$action('Cost_type_model', $id);
	}

	/***
	public function inbound($action = 'list', $id = false)
	{
		$this->data['site']['method'] = __FUNCTION__;
		//初始化控制器方法变量
		$this->data['sub_menu_index'] = 3;
		//
		$this->data['index']['title'] = '入库原因';
		//类目管理子菜单
		$this->data['index']['menus'] = array(
				array(
					'url'	=>	$this->data['site']['root'] . 'system/inbound/list/',
					'name'	=>	'可选入库原因'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/inbound/listRecover/',
					'name'	=>	'回收入库原因'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/inbound/create/',
					'name'	=>	'创建入库原因')
			);
		$action = '_' . $action;
		$this->$action('Inbound_type_model', $id);
	}

	public function outbound($action = 'list', $id = false)
	{
		$this->data['site']['method'] = __FUNCTION__;
		//初始化控制器方法变量
		$this->data['sub_menu_index'] = 4;
		//
		$this->data['index']['title'] = '出库原因';
		//类目管理子菜单
		$this->data['index']['menus'] = array(
				array(
					'url'	=>	$this->data['site']['root'] . 'system/outbound/list/',
					'name'	=>	'可选出库原因'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/outbound/listRecover/',
					'name'	=>	'回收出库原因'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/outbound/create/',
					'name'	=>	'创建出库原因')
			);
		$action = '_' . $action;
		$this->$action('Outbound_type_model', $id);
	}
	***/

	private function _list($model)
	{
		$this->data['title'] = '可选货品类目';
		$this->data['index']['list'] = 'enable';
		$this->data['types'] = $this->$model->_list('enable');
		return $this->load->view_('system_type_list_view', $this->data);
	}

	private function _listRecover($model)
	{
		$this->data['title'] = '回收货品类目';
		$this->data['index']['list'] = 'disable';
		$this->data['types'] = $this->$model->_list('disable');
		return $this->load->view_('system_type_list_view', $this->data);
	}

	private function _create($model)
	{
		if ( $this->forbid_resubmit->judge_submit($this->input->post('rand')) )
		{
			if ( $this->$model->create() )
			{
				$ctrl_url = strtolower(array_shift(explode('_', $model)));
				header("Location: {$this->data['site']['root']}system/" . $ctrl_url . "/list/");
				return;
			}
		}
		$this->data['title'] = '新建货品类目';
		//生成表单随机数
		$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
		$this->load->view_('system_type_create_view', $this->data);
	}

	private function _update($model, $id)
	{
		//判断类目是否存在
		if ($this->data['form']['type'] = $this->$model->getOne($id))
		{
			if ($this->forbid_resubmit->judge_submit($this->input->post('rand')))
			{
				//提交成功，摧毁原始值
				unset($this->data['form']['type']['name']);
				if (!$this->$model->update())
				{
					$this->data['form']['result'] = '更新失败';
				}
				else
				{
					//更新成功后更新数据
					$this->data['form']['type'] = $this->$model->getOne($id);
					$this->data['form']['result'] = '编辑完成';
				}
			}
			$this->data['title'] = '更新类目';
			//生成随机数
			$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
			return $this->load->view_('system_type_update_view', $this->data);
		}
		$this->data['error'] = '不存在此类目';
		return $this->load->view_('error_500_view', $this->data);
	}

	private function _del($model, $id)
	{
		$id = $this->input->post('id');
		foreach ($id as $v)
		{
			$this->$model->del($v);
			$this->data['ajax_request'] = 'DEL_SUCCESS';
		}
		return $this->load->view('ajax_request', $this->data);
	}

	private function _recover($model, $id)
	{
		$id = $this->input->post('id');
		foreach ($id as $v)
		{
			$this->$model->recover($v);
			$this->data['ajax_request'] = 'RECOVER_SUCCESS';
		}
		return $this->load->view('ajax_request', $this->data);
	}

/***
	public function _goods($type = 'list', $id = false)
	{
		//初始化控制器方法变量
		$this->data['sub_menu_index'] = 1;
		//类目管理子菜单
		$this->data['index']['menus'] = array(
				array(
					'url'	=>	$this->data['site']['root'] . 'system/goods/list/',
					'name'	=>	'可选类目'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/goods/listRecover/',
					'name'	=>	'回收类目'),
				array(
					'url'	=>	$this->data['site']['root'] . 'system/goods/create/',
					'name'	=>	'创建类目')
			);
		//识别方法类型
		switch ($type) {
			case 'list':
				$this->_goodsTypeList('enable');
				break;
			case 'listRecover':
				$this->_goodsTypeList('disable');
				break;
			case 'create':
				$this->_goodsTypeCreate();
				break;
			case 'update' :
				$this->_goodsTypeUpdate($id);
				break;
			case 'del' :
				$this->_goodsTypeDel();
				break;
			case 'recover' :
				$this->_goodsTypeRecover();
				break;
			
			default:
				$this->data['error'] = '拒绝访问';
				$this->load->view_('error_500_view', $this->data);
				break;
		}
	}

	private function _goodsTypeCreate()
	{
		if ( $this->forbid_resubmit->judge_submit($this->input->post('rand')) )
		{
			if ( $this->Goods_type_model->create() )
			{
				header("Location: {$this->data['site']['root']}system/goods/list/");
				return;
			}
		}
		$this->data['title'] = '新建货品类目';
		//生成表单随机数
		$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
		$this->load->view_('system_goods_type_create_view', $this->data);
	}

	private function _goodsTypeList($status)
	{
		if ('enable' == $status)
		{
			$this->data['title'] = '可选货品类目';
			$this->data['index']['list'] = 'enable';
		}
		else
		{
			$this->data['title'] = '回收货品类目';
			$this->data['index']['list'] = 'disable';
		}
		$this->data['goods_types'] = $this->Goods_type_model->_list('enable');
		return $this->load->view_('system_goods_type_list_view', $this->data);
	}

	private function _goodsTypeDel()
	{
		
	}

	private function _goodsTypeUpdate($id)
	{	
		//判断类目是否存在
		if ($this->data['form']['type'] = $this->Goods_type_model->getOne($id))
		{
			if ($this->forbid_resubmit->judge_submit($this->input->post('rand')))
			{
				//提交成功，摧毁原始值
				unset($this->data['form']['type']['name']);
				if (!$this->Goods_type_model->update())
				{
					$this->data['form']['result'] = '更新失败';
				}
				else
				{
					//更新成功后更新数据
					$this->data['form']['type'] = $this->Goods_type_model->getOne($id);
					$this->data['form']['result'] = '编辑完成';
				}
			}
			$this->data['title'] = '更新类目';
			//生成随机数
			$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
			return $this->load->view_('system_goods_type_update_view', $this->data);
		}
		$this->data['error'] = '不存在此类目';
		return $this->load->view_('error_500_view', $this->data);
	}


**/


}